﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using BLL;
namespace UI
{
    public partial class Form1 : Form
    {
        //套餐
        public DataTable tcTable;
        //项目
        public DataTable xmTable;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //体检套餐combox
            tcTable = BLL.UserBLL.SelectAllTC();
            for (int i = 0; i < tcTable.Rows.Count; i++)
            {
                comboBox_tc.Items.Add(tcTable.Rows[i][1]);
            }
            comboBox_tc.SelectedIndex = 0;
            //体检项目combox
            xmTable = BLL.UserBLL.SelectAllXM();
            for (int i = 0; i < xmTable.Rows.Count; i++)
            {
                comboBox_xm.Items.Add(xmTable.Rows[i][1]);
            }
            comboBox_xm.SelectedIndex = 0;
            ///根据套餐combox的索引去 查询tablel里面的值 去查询盖套餐所有项目
            int selIndex = comboBox_tc.SelectedIndex;
            Model.TC tc = new Model.TC();
           tc.TCID= Convert.ToInt32(tcTable.Rows[selIndex][0]);
           dataGridView_view.DataSource = BLL.UserBLL.SelectAllXMbyTCID(tc);

        }

        private void button_tcadd_Click(object sender, EventArgs e)
        {
            String tcName = textBox_tc.Text;
            Model.TC tc = new Model.TC();
            tc.TCNAME = tcName;
            if (BLL.UserBLL.addTC(tc) == 1)
            {
                MessageBox.Show("增加套餐成功！");
                //刷新体检套餐
                //体检套餐combox
                comboBox_tc.Items.Clear();
                //这个成员属性重新赋值
                tcTable = BLL.UserBLL.SelectAllTC();
                for (int i = 0; i < tcTable.Rows.Count; i++)
                {
                    comboBox_tc.Items.Add(tcTable.Rows[i][1]);
                }
                comboBox_tc.SelectedIndex = 0;
            }
            else {
                MessageBox.Show("增加套餐失败");
            }
        }

        private void comboBox_tc_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///根据套餐combox的索引去 查询tablel里面的值 去查询盖套餐所有项目
            int selIndex = comboBox_tc.SelectedIndex;
            Model.TC tc = new Model.TC();
            tc.TCID = Convert.ToInt32(tcTable.Rows[selIndex][0]);
            dataGridView_view.DataSource = BLL.UserBLL.SelectAllXMbyTCID(tc);
        }

        private void button_xmadd_Click(object sender, EventArgs e)
        {
            int tcindex = comboBox_tc.SelectedIndex;
            int xmindex = comboBox_xm.SelectedIndex;
            Model.TCrelativeXM txm = new Model.TCrelativeXM();
            txm.TCID =Convert.ToInt32( tcTable.Rows[tcindex][0]);
            txm.XMID =Convert.ToInt32( xmTable.Rows[xmindex][0]);
            if (BLL.UserBLL.addXMtoTC(txm) == 1)
            {
                MessageBox.Show("添加成功！");
                ///根据套餐combox的索引去 查询tablel里面的值 去查询盖套餐所有项目
                int selIndex = comboBox_tc.SelectedIndex;
                Model.TC tc = new Model.TC();
                tc.TCID = Convert.ToInt32(tcTable.Rows[selIndex][0]);
                dataGridView_view.DataSource = BLL.UserBLL.SelectAllXMbyTCID(tc);
            }
            else {
                MessageBox.Show("添加失败！");
            }
        }
    }
}
